﻿Imports System.ServiceModel
Imports System.ServiceModel.Activation
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.IO

<ServiceContract(Namespace:="")> _
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
Public Class ServiceWCF

  Structure TestResults
    Dim Err As String
    Dim Message As String
  End Structure

  Structure GetDataResults
    Dim Err As String
    Dim Contacts As List(Of Contact)
  End Structure

  <OperationContract()> _
Public Function Test(ByVal sData As String) As TestResults
    Dim result As TestResults = New TestResults

    Try
      result.Err = String.Empty

      result.Message = String.Format("{0} {1} {2}", sData, Now.ToLongDateString, GetVersion)

    Catch ex As Exception
      result.Err = ex.ToString
    End Try

    Logging(String.Format("Call:{0} Par:{1} From:{2}({3}) Who:{4}", "Ws Test", sData, System.Web.HttpContext.Current.Request.UserHostName, System.Web.HttpContext.Current.Request.UserHostAddress, System.Web.HttpContext.Current.User.Identity.Name))

    Return result

  End Function

  Public Function GetVersion() As String
    Dim result As String = ""
    Try
      Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
      result = Split(asm.FullName, ",")(1)
    Catch ex As Exception
    End Try

    Return (result)

  End Function

  Dim MyMutex As New System.Threading.Mutex

  Private Sub Logging(ByVal Smessage As String)

    'Exit Sub

    If Not My.Settings.Logging Then Exit Sub

    MyMutex.WaitOne()

    Try

      Dim flLogName As String = My.Settings.LoggingPath

      If flLogName <> "" Then

        Dim fi As FileInfo = New FileInfo(System.Web.HttpContext.Current.Server.MapPath("") & "\" & flLogName & ".dat")

        Dim sw As StreamWriter
        If fi.Exists = False Then
          sw = fi.CreateText()
        Else
          sw = fi.AppendText()
        End If

        sw.WriteLine("{0}:{1} - {2}", Now.ToShortDateString, Now.ToLongTimeString, Smessage)
        sw.Flush()
        sw.Close()

        If fi.Exists AndAlso fi.Length > 1000000 Then
          Dim di As New DirectoryInfo(String.Format("{0}\BackUp\{1:0000}{2:00}{3:00}", fi.DirectoryName, Now.Year, Now.Month, Now.Day).ToString)
          If Not di.Exists Then di.Create()
          fi.MoveTo((String.Format("{0}\LoggingAt_{1:00}{2:00}{3:00}.log", di.FullName, Now.Hour, Now.Minute, Now.Second).ToString))
        End If

      End If

    Catch ex As Exception

    End Try

    MyMutex.ReleaseMutex()

  End Sub

  <OperationContract()> _
Public Function GetData(ByVal sData As String) As GetDataResults
    Dim result As GetDataResults = New GetDataResults

    Try
      result.Err = String.Empty
      Dim Datacls As New DataClassesAdventureDataContext

      Dim foundContacts = From DataContacts In Datacls.Contacts Where DataContacts.LastName.StartsWith(sData) Select DataContacts

      result.Contacts = foundContacts.ToList()

    Catch ex As Exception
      result.Err = ex.ToString
    End Try

    Logging(String.Format("Call:{0} Par:{1} From:{2}({3}) Who:{4}", "Ws Get Data", sData, System.Web.HttpContext.Current.Request.UserHostName, System.Web.HttpContext.Current.Request.UserHostAddress, System.Web.HttpContext.Current.User.Identity.Name))

    Return result
  End Function

    ' Add more operations here and mark them with <OperationContract()>
End Class
